package site.psvm.phoneinfo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.psvm.phoneinfo.common.bean.dto.ListDto;
import site.psvm.phoneinfo.common.bean.mongoModel.PhoneInfoModel;
import site.psvm.phoneinfo.common.util.Resp;
import site.psvm.phoneinfo.service.PhoneInfoService;

import java.util.List;

@RestController
public class PhoneInfoController extends BaseController{

    private static final Logger logger =  LoggerFactory.getLogger(PhoneInfoController.class);
    private PhoneInfoService phoneInfoService;

    public PhoneInfoController(PhoneInfoService phoneInfoService) {
        this.phoneInfoService = phoneInfoService;
    }

    //查询最近更新的10条手机信息
    @RequestMapping("/indexPage")
    public Resp<List<PhoneInfoModel>> queryNewCreatePhoneInfo() {
        try{
            ListDto<PhoneInfoModel> result = phoneInfoService.queryRecentUpdatesPhoneInfo();
            if ("001".equals(result.getResultCode())) {
                return Resp.ok(result.getList());
            }else {
                return Resp.error(result.getResultMsg());
            }
        }catch(Exception e){
            logger.error("PhoneInfoController,queryNewCreatePhoneInfo,error,",e);
            return Resp.error();
        }
    }
}
